package com.nutquest.client;

import java.util.*;
import javax.swing.*;

import com.nutquest.common.dataobjects.Player;
import com.nutquest.server.GameServer;

/**
 * The Class readFromServer.
 */
public class readFromServer extends Thread
{
	static final String TERRAIN_SENT = GameServer.SEND_TERRAIN_COMMAND;

   /** The c. */
   NutQuestClient c;

   // constructor
   /**
    * Instantiates a new read from server.
    * 
    * @param cc
    *           the NutQuestInterface GUI
    */
   readFromServer(NutQuestClient cc)
   {
      c = cc;
   }

   /*
    * (non-Javadoc)
    * 
    * @see java.lang.Thread#run()
    */
   public void run()
   {
      String s;

      while (true)
      {

         if (NutQuestClient.logout)
         {
            return;
         }

         s = NutQuestClient.readString();

         // TODO: debug
         if (s == null)
         {
            return;
         }

         if (s.startsWith("Players"))
         {
            NutQuestClient.readPlayers();

            NutQuestClient.lobbyChatTArea.setText("Connected as "
                  + NutQuestClient.player.getName());
            c.frame.setTitle("NutQuest Lobby - " + c.player.getName()
                  + " - Connected to " + c.host);
            NutQuestClient.connected = true;

            NutQuestClient.list.clear();
            for (int i = 0; i < NutQuestClient.players.size(); i++)
            {
               Player pl = NutQuestClient.players.get(i);
               NutQuestClient.list.addElement(pl.getName());
            }

            System.out.print("List updated: New names: ");
            for (int i = 0; i < NutQuestClient.list.size(); i++)
            {
               System.out.print(NutQuestClient.list.get(i) + " ");
            }
            System.out.println();
         }
         else if (s.startsWith("Map"))
         {
        	 c.readMap();
         }
         
         else if (s.equals(TERRAIN_SENT))
         {
        	 NutQuestClient.readTerrain();
         }

         // else if (s.startsWith("List")) {
         //
         // NutQuestInterface.lobbyChatTArea.setText("Connected as "
         // + NutQuestInterface.player.getName());
         // c.frame.setTitle("NutQuest Lobby - " + c.player.getName()
         // + " - Connected to " + c.host);
         // // connected = true;
         // NutQuestInterface.connected = true;
         //
         // NutQuestInterface.list.clear();
         // String nextNick = "";
         //
         // StringTokenizer st = new StringTokenizer(s.substring(5,
         // s.length()), ", ");
         //
         // String temp = null;
         // while (st.hasMoreTokens()) {
         // temp = st.nextToken();
         //
         // NutQuestInterface.list.addElement(replace(temp, ";", ""));
         // }
         //
         // System.out.print("List updated: New names: ");
         // for (int i = 0; i < NutQuestInterface.list.size(); i++) {
         // System.out.print(NutQuestInterface.list.get(i) + " ");
         // }
         // System.out.println();
         // }

         else if (s.startsWith("Receive"))
         {
            NutQuestClient.lobbyChatTArea
                  .setText(NutQuestClient.lobbyChatTArea.getText() + "\n"
                        + s.substring(8, s.length()));

            NutQuestClient.lobbyChatTArea
                  .setCaretPosition(NutQuestClient.lobbyChatTArea.getText()
                        .length());
         }
         // Private msg
         // Send private message
         else if (s.startsWith("Private Receive"))
         {
            NutQuestClient.lobbyChatTArea
                  .setText(NutQuestClient.lobbyChatTArea.getText() + "\n"
                        + "Private message: " + s.substring(14, s.length()));
            NutQuestClient.lobbyChatTArea
                  .setCaretPosition(NutQuestClient.lobbyChatTArea.getText()
                        .length());
         }

         else if (s.startsWith("NewNick"))

         {

            // TODO

            // NutQuestInterface.lobbyChatTArea.setText("");
            // String newnick = JOptionPane.showInputDialog(null,
            // "Go back and enter Username:"); //TODO
            // NutQuestInterface.connected = false;

            System.exit(0);

            /*
             * if (newnick != null) {
             * 
             * NutQuestInterface.userName = newnick;
             * 
             * NutQuestInterface.send("Login: " + newnick);
             * 
             * System.exit(0); }
             */
         }
         System.out.println(s);
      }
   }

   /**
    * Replace.
    * 
    * @param str
    *           the str
    * @param pattern
    *           the pattern
    * @param replace
    *           the replace
    * @return the string
    */
   static String replace(String str, String pattern, String replace)
   {
      int s = 0;
      int e = 0;
      StringBuffer result = new StringBuffer();
      while ((e = str.indexOf(pattern, s)) >= 0)
      {
         result.append(str.substring(s, e));
         result.append(replace);
         s = e + pattern.length();
      }
      result.append(str.substring(s));
      return result.toString();
   }
}